package com.gmzg.gmzgnews.me.view;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.gmzg.gmzgnews.MainApplication;
import com.gmzg.gmzgnews.R;
import com.gmzg.gmzgnews.me.model.User;

import org.greenrobot.eventbus.EventBus;
import org.xutils.DbManager;
import org.xutils.db.sqlite.SqlInfo;
import org.xutils.db.table.DbModel;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity implements TextWatcher {

    @BindView(R.id.login_toolbar)
    Toolbar mLoginToolbar;
    @BindView(R.id.login_phoneNumber_et)
    EditText mLoginPhoneNumberEt;
    @BindView(R.id.login_password_et)
    EditText mLoginPasswordEt;
    @BindView(R.id.login_btn)
    Button mLoginBtn;
    @BindView(R.id.activity_login)
    RelativeLayout mActivityLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        setSupportActionBar(mLoginToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mLoginPasswordEt.addTextChangedListener(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    @OnClick(R.id.login_btn)
    public void onClick() {
        String phone = mLoginPhoneNumberEt.getText().toString();
        String pwd = mLoginPasswordEt.getText().toString();
        DbManager db = ((MainApplication) getApplication()).getDb();
        try {
            SqlInfo sql = new SqlInfo("select * from user where phonenumber = "+phone);
            List<DbModel> dbModelAll = db.findDbModelAll(sql);
            if (dbModelAll != null&&dbModelAll.size()!=0) {
                DbModel dbModel = dbModelAll.get(0);
                String password = dbModel.getString("password");
                String image = dbModel.getString("image");
                String username = dbModel.getString("username");
                if (password.equals(pwd)){
                    //将登陆信息保存到sharedPreference中
                    SharedPreferences sp = getSharedPreferences(MeFragment.SP_USER, MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString(MeFragment.USER_IMAGE,image);
                    editor.putString(MeFragment.USERNAME,username);
                    editor.apply();
                    EventBus.getDefault().post(new User());
                    finish();
                }
            }else {
                Toast.makeText(this, "该账号不存在", Toast.LENGTH_SHORT).show();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        mLoginBtn.setBackgroundColor(Color.RED);
        mLoginBtn.setClickable(true);
    }
}
